package persistencia;

import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

import logica.Categoria;


public class PersistirCategoria extends Persistir{

	public PersistirCategoria(){}
	
/*******************************************************************************************************************/	
/******************************************************************************************************************/
	/*------------------------  ALTA DE CATEGORIA  --------------------------*/

	public boolean existeCategoria (Categoria c){
		boolean existe = true;
		try{
			Statement consulta = getConexion().createStatement();
			ResultSet rs= consulta.executeQuery("SELECT Nombre FROM Categoria WHERE Nombre = '"+c.getNombre().toUpperCase()+"'");
			
			if(!rs.next()){
				existe = false;
			}
			consulta.close();
		}
		catch (Exception e) {
			JOptionPane.showMessageDialog(null,"ERROR AL CONECTAR LA BASE DE DATOS EN existeCategoria");
		}
		
		return existe;
	}
	
/*-----------------------------------------------------------------------------------------------------------------*/
	
	public void addCategoria(Categoria c) {

		try {
			Statement agregar = getConexion().createStatement();
			agregar.execute("INSERT INTO Categoria (Nombre) VALUES ('"+ c.getNombre().toUpperCase()+"');");
			agregar.close();
		} 
		catch (Exception e) {
			JOptionPane.showMessageDialog(null,"ERROR AL CONECTAR LA BASE DE DATOS EN addCategoria");
		}
	} 

	
	
/*******************************************************************************************************************/	
/******************************************************************************************************************/
	/*------------------------  CONSULTAR JUEGO  y ALTA DE JUEGO--------------------------*/

	public ResultSet getCategorias(){
		ResultSet categorias = null;
		try {
			Statement obtener = getConexion().createStatement();
			ResultSet rs = obtener.executeQuery("SELECT Nombre FROM Categoria");
			categorias = rs;
		}
		catch (Exception e) {
			JOptionPane.showMessageDialog(null,"ERROR AL CONECTAR LA BASE DE DATOS EN getCategorias");
		}
		return categorias;
	}
	
	
/*******************************************************************************************************************/	
/******************************************************************************************************************/
	/*------------------------   -------------------*/
	
}
	